package tasktype

import (
    "cing.io/cing-api/task"
    "context"

    "google.golang.org/grpc"

    "cing.io/cing-api/planner"
    "cing.io/cing/internal/builtins/tasktype/cron"
    "cing.io/cing/internal/builtins/tasktype/fixedinterval"
    "cing.io/cing/internal/builtins/tasktype/fixedrate"
)

// NOTICE: the built-in task type only supports solo singing, such as OneRandomSinger and TheFirstSinger
var (
    builtinTaskTypeMap = map[string]*task.TaskType{
        cron.Name:          cron.TaskType(),
        fixedrate.Name:     fixedrate.TaskType(),
        fixedinterval.Name: fixedinterval.TaskType(),
    }

    builtinPlannerMap = map[string]planner.CingPlannerClient{
        cron.Name:          &wrapper{CingPlannerServer: &cron.CingPlannerServer{}},
        fixedinterval.Name: &wrapper{CingPlannerServer: &fixedinterval.CingPlannerServer{}},
        fixedrate.Name:     &wrapper{CingPlannerServer: &fixedrate.CingPlannerServer{}},
    }
)

func GetTaskType(taskTypeName string) *task.TaskType {
    return builtinTaskTypeMap[taskTypeName]
}

func GetPlanner(taskTypeName string) planner.CingPlannerClient {
    return builtinPlannerMap[taskTypeName]
}

type wrapper struct {
    planner.CingPlannerClient
    planner.CingPlannerServer
}

func (w *wrapper) TaskTypeEcho(ctx context.Context, in *planner.TaskTypeEchoRequest, _ ...grpc.CallOption) (*planner.TaskTypeEchoResponse, error) {
    return w.CingPlannerServer.TaskTypeEcho(ctx, in)
}
func (w *wrapper) OnTaskCreate(ctx context.Context, in *planner.OnTaskCreateRequest, _ ...grpc.CallOption) (*planner.OnTaskCreateResponse, error) {
    return w.CingPlannerServer.OnTaskCreate(ctx, in)
}
func (w *wrapper) OnTaskUpdate(ctx context.Context, in *planner.OnTaskUpdateRequest, _ ...grpc.CallOption) (*planner.OnTaskUpdateResponse, error) {
    return w.CingPlannerServer.OnTaskUpdate(ctx, in)
}
func (w *wrapper) OnTaskDelete(ctx context.Context, in *planner.OnTaskDeleteRequest, _ ...grpc.CallOption) (*planner.OnTaskDeleteResponse, error) {
    return w.CingPlannerServer.OnTaskDelete(ctx, in)
}
func (w *wrapper) OnSingingStart(ctx context.Context, in *planner.OnSingingStartRequest, _ ...grpc.CallOption) (*planner.OnSingingStartResponse, error) {
    return w.CingPlannerServer.OnSingingStart(ctx, in)
}
func (w *wrapper) OnSingingEnd(ctx context.Context, in *planner.OnSingingEndRequest, _ ...grpc.CallOption) (*planner.OnSingingEndResponse, error) {
    return w.CingPlannerServer.OnSingingEnd(ctx, in)
}
func (w *wrapper) ListenEvents(context.Context, *planner.ListenEventsRequest, ...grpc.CallOption) (planner.CingPlanner_ListenEventsClient, error) {
    return nil, nil
}
